<?php defined('SYSPATH') or die('No direct script access.');

class Model_Supplier extends ORM
{ 
    protected $_table_name = 'suppliers';  
    const SUPPLIERS_PER_PAGE = 15;
    
    public function get_suppliers($category = null, $page = null, $get_count = false, $order_by = 'id')
    {        
        $query = $this->select();
        if ($category !== null) {
            $category_id = $category;
            if (is_object($category)) {
                $category_id = $category->id;
            }
            $query->join('supplier_categories', 'LEFT')
                  ->on('supplier_categories.supplier_id', '=' , 'supplier.id');
        }
        $query->where('supplier_categories.category_id', '=', $category_id);
               
        if ($page) {
            $count = $this->get_suppliers_per_page();
            $offset = ($page - 1) * $count;
            $query->offset($offset)->limit($count);    
        }
        
        if ($order_by) {
            $query->order_by($order_by, 'ASC');   
        }
        
        if ($get_count) {
            return $query->count_all();   
        }
        
        
        return $query->find_all();       
    }
    
    public function get_suppliers_per_page()
    {
        return self::SUPPLIERS_PER_PAGE;
    }
     
    public function add_supplier($name, $type, $volume, $min_order, $payment_options, $delivery_methods,
            $legal_person, $payment_details, $description, $buy_opt, $nalog_system, $categories, $phone, $address, $worktime, $website, $html, $url_suppliers)
    {
        $this->name             = $name;
        $this->type             = $type;
        $this->volume           = $volume;
        $this->min_order        = $min_order;
        $this->payment_options  = $payment_options;
        $this->delivery_methods = $delivery_methods;
        $this->legal_person     = $legal_person;
        $this->payment_details  = $payment_details;
        $this->description      = $description;
        $this->buy_opt          = $buy_opt;
        $this->nalog_system     = $nalog_system;
        $this->categories       = $categories;
        $this->phone            = $phone;
        $this->address          = $address;
        $this->worktime         = $worktime;
        $this->website          = $website;
        $this->html             = $html;
        $this->url_suppliers    = $url_suppliers; 
        
        try {
            $this->save();
        } catch (Database_Exception $e) {
            if (strstr($e->getMessage(), 'Duplicate entry') !== false) {
                return null;
            }
            die($e->getMessage());
        }
        
        return $this;  
    }
    
    public static function get_suppliers_by_goods($goods)
    {
        $ids = array();
        foreach($goods as $item){
            $ids[$item->supplier_id] = $item->supplier_id;                    
        }
        $ids = array_values($ids);
        if(!empty($ids))
            $suppliersData = ORM::factory('supplier')
                                    ->where('id', 'IN', $ids)
                                    ->find_all();
        else
            return false;
        
        $suppliers = $_suppliers = array();
        foreach($suppliersData as $supplier){
            $_suppliers[$supplier->id] = $supplier;    
        }
        foreach ($ids as $id) {
            $suppliers[$id] = $_suppliers[$id];
        }
        /*echo "<pre>";
        print_r($suppliers);
        die();*/  
        return $suppliers;
    }
    
    public static function get_words_name($name){
        $name = mb_strtolower($name);
        $name = html_entity_decode($name, ENT_QUOTES, "UTF-8");
        $name = strip_tags($name);
        $name = preg_replace('/[a-zа-яё0-9-]+\./iu', ' ', $name);
        $name = preg_replace('/[^a-zа-яё0-9- ]+/iu', ' ', $name);
        $name = preg_replace('/[0-9-]+/iu', ' ', $name);
        $name = preg_replace('/ [0-9]+( |$)/iu', ' ', $name);
        $name = trim($name);
        $tmp = explode(" ", $name);
        $words = array();
        foreach($tmp as $w){
            if($w != "" && mb_strlen($w) > 3)
                $words[] = $w;
        }
        
        return $words;
    }
    
    public function get_supplier_phone()
    {
        /*if ($this->active_phone) {
            return '+7 495 255-32-85, добавочный номер - '.$this->id;    
        } */
        return $this->phone;    
    }
    
    public function get_supplier_email()
    {
        return null;
        if ($this->active_email) {
            return 'company'.$this->id.'@optzilla.ru';    
        }
        return $this->email;   
    }
        
}